/* import { terser } from 'rollup-plugin-terser';
import commonjs from 'rollup-plugin-commonjs';
import typescript from 'rollup-plugin-typescript2';
import clear from 'rollup-plugin-clear';
import resolve from 'rollup-plugin-node-resolve';
import nodePolyfills from 'rollup-plugin-node-polyfills';
import babelPlugin from 'rollup-plugin-babel';
import path from 'path';

const pkgCore = import.meta.resolve('./packages/core/package.json');


export default [{
  input: 'packages/core/src/index.ts',
  
  external: [
    /^three\/src\//
  ],
  output: [
    {
      file: path.basename(pkgCore.main),
      format: 'umd',
      name: 'tezero',
      sourcemap: true,
      exports: 'named',
    },
    {
      file: path.basename(pkgCore.module),
      format: 'es',
      name: 'tezero',
      sourcemap: true,
      exports: 'named',
      inlineDynamicImports: true,
    },
  ],

  plugins: [
    clear({
      targets: ["dist"],
    }), 
    typescript({ tsconfig: './tsconfig.json' }),
    commonjs(), 
    resolve({ browser: true }), 
    terser(), 
    nodePolyfills(), 
    babelPlugin({
      exclude: "node_modules/**",
      runtimeHelpers: true,
    }),
  ],
}]
 */

import { terser } from 'rollup-plugin-terser';
import commonjs from 'rollup-plugin-commonjs';
import typescript from 'rollup-plugin-typescript2';
import resolve from 'rollup-plugin-node-resolve';
import nodePolyfills from 'rollup-plugin-node-polyfills';
import babelPlugin from 'rollup-plugin-babel';
import json from 'rollup-plugin-json';
import clear from 'rollup-plugin-clear';
import { createRequire } from 'module'
const require = createRequire(import.meta.url)

const packages = ['core','layout'].map(pkgName => {
  const pkg = require(`./packages/${pkgName}/package.json`)
  
  const baseConfig = {
    input: `packages/${pkgName}/src/index.ts`,
    //external: Object.keys(pkg.dependencies || {}),
    output: [
      { 
        file: pkg.main, 
        format: 'umd', 
        name: `tezero.${pkgName}`,
        globals: pkgName !== 'core' ? { three: 'Tezero.THREE' } : {},
        sourcemap: true
      },
      { 
        file: pkg.module, 
        format: 'es', 
        sourcemap: true 
      }
    ],
    plugins: [
      json(),
      clear({
        targets: ["dist"],
      }), 
      typescript({ tsconfig: './tsconfig.json' }),
      commonjs(), 
      resolve({ browser: true }), 
      terser(), 
      nodePolyfills(), 
      babelPlugin({
        exclude: "node_modules/**",
        runtimeHelpers: true,
      }),
    ]
  }

  // 特殊处理非core包的external
  if (pkgName !== 'core') {
    baseConfig.external = [
      // ...Object.keys(pkg.dependencies || {}),
      // ...Object.keys(pkg.peerDependencies || {}),
      'three', // 明确排除three.js
      'localforage',
      '@tweenjs/tween.js',
      /^three\// // 排除所有three的子模块
    ]
  }

  return baseConfig
  
})

export default packages